package org.acy.train.member.service.impl;

import jakarta.annotation.Resource;
import org.acy.train.common.exception.BusinessException;
import org.acy.train.member.domain.Member;
import org.acy.train.member.mapper.MemberMapper;
import org.acy.train.member.req.MemberRegisterReq;
import org.acy.train.member.service.MemberService;
import org.springframework.stereotype.Service;

@Service
public class MemberServiceImpl implements MemberService {

    @Resource
    MemberMapper memberMapper;

    @Override
    public Integer memberCount() {
        return memberMapper.count(where -> {
        });
    }

    @Override
    public Long register(MemberRegisterReq req) {
        if (checkExists(req.getMobile())) {
            throw new BusinessException("手机号已存在,注册失败");
        }
        Member member = new Member();
        member.setMobile(req.getMobile());
        memberMapper.save(member);
        return member.getId();
    }

    /**
     * 校验是否存在
     *
     * @param mobile 手机号
     * @return 校验结果
     */
    private boolean checkExists(String mobile) {
        Integer count = memberMapper.count(where -> where.eq(Member::getMobile, mobile));
        return count > 0;
    }
}
